package huawei.js2025;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 找等值元素
 */
public class Question44 {

    public static class Point{
        int x;
        int y;

        public Point(int x, int y){
            this.x = x;
            this.y = y;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<List<Integer>> list = new ArrayList<>();

        int x = 0;

        Map<Integer, List<Point>> map = new HashMap<>();

        String line = in.nextLine();

        do {
            List<Integer> collect = Arrays.stream(line.split(" ")).map(String::trim).map(Integer::parseInt).collect(Collectors.toList());
            for (int i = 0; i < collect.size(); i++) {
                int v = collect.get(i);
                if (map.containsKey(v)) {
                    List<Point> points = map.get(v);
                    points.add(new Point(x, i));
                } else {
                    List<Point> points = new ArrayList<>();
                    points.add(new Point(x, i));
                    map.put(v, points);
                }
            }

            list.add(collect);
            x+=1;

            line = in.nextLine();
        } while (!line.isEmpty());

        int[][] ints = new int[list.size()][list.get(0).size()];
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                if (i == 1 && j == 0) {
                    System.out.println();
                }
                Integer v = list.get(i).get(j);
                if (map.get(v).size() == 1) {
                    ints[i][j] = -1;
                } else {
                    List<Point> points = map.get(v);
                    for (Point p : points) {
                        if (p.x != i || p.y != j) {
                            int distance = Math.abs(p.x - i) + Math.abs(p.y - j);
                            if (ints[i][j] == 0) {
                                ints[i][j] = distance;
                            } else {
                                ints[i][j] = Math.min(ints[i][j], distance);
                            }
                        }
                    }
                }
            }
        }

        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                if (j != ints[i].length - 1) {
                    System.out.print(ints[i][j] + " ");
                } else {
                    System.out.print(ints[i][j]);
                }
            }
            System.out.println();
        }
    }
}
